package edu.msse.signup.ui;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import edu.msse.signup.service.EventService;
import edu.msse.signup.service.PositionService;
import edu.msse.signup.service.VolunteerService;
import edu.msse.signup.domain.Event;
import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.Users;

public class PositionHandler {

	private PositionService positionService;
	private VolunteerService volunteerService;
	private EventService eventService;
	private String numberOfVolunteers;
	private ApplicationService applicationService;
	private String name;
	private String description;
	private Position currentPosition;
	private String currentEventId;
	private String currentPositionId;
	private boolean isAddedToNewEvent = true;
	
	public PositionHandler() {
		
	}
	

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}
	
	public String addToEvent(){
		Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		currentEventId = (String) map.get("eventDetailForm:currentEventId");
		if (currentEventId == null){
			setAddedToNewEvent(false);
			currentEventId = (String) map.get("eventForm:currentEventId");		
		}
		
		return "newPosition";
	}
	
	
	public String createNew()
	{
		if (!applicationService.validateNumber(numberOfVolunteers))
			return null;
	
		try{
			Event event = getCurrentEvent();
			currentPosition = PositionService.createPosition(name, description, Integer.parseInt(numberOfVolunteers), event);
			EventService.addPosition(event, currentPosition);
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentPositionId", String.valueOf(currentPosition.getPositionId()));
			
		} catch (Exception x) {
            applicationService.addErrorMessage(x.getMessage());
            return null;
        }
		
		return isAddedToNewEvent() ? "newTimeslot" :"myEvents";
	}
	
	public List getTimeSlots(){
		return positionService.getPositionTimeslots(getCurrentPosition());
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Event getCurrentEvent() {
		Event currentEvent = (Event)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("currentEvent");
		if(currentEvent != null)
			return currentEvent;
		
		String eId = getCurrentEventId();
		if (eId != null){
			int eventId = Integer.parseInt(eId);
			currentEvent = eventService.getEvent(eventId);
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentEventId", eventId);						
		}
		
		return currentEvent;
	}
	
	public String removePosition(){
		String id =  FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("currentPositionId");
		currentPosition = positionService.getPosition(Integer.parseInt(id));
//		List users = positionService.getUsers(currentPosition);
//		Iterator i = users.iterator();
//		while(i.hasNext()){
//			Users user = (Users)i.next();
//			volunteerService.unFillPosition(user.getUserId(), currentPosition.getPositionId());
//		}
		boolean status = PositionService.delete(currentPosition);
		String redirect = "myEvents";
		if (status == false)
			redirect = "";
		else{
			applicationService.addInfoMessage("Position \"" + currentPosition.getName() + "\" has been deleted.");
		}
		
		return redirect;
	}

	public Position getCurrentPosition() {
		return currentPosition;
	}


	public String getNumberOfVolunteers() {
		return numberOfVolunteers;
	}

	public List getVolunteers(){
		return positionService.getUsers(getCurrentPosition());
		
	}
	

	public void setNumberOfVolunteers(String numberOfVolunteers) {
		this.numberOfVolunteers = numberOfVolunteers;
	}


	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}


	public String getCurrentEventId() {
		if (currentEventId == null)
			currentEventId =(String)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("currentEventId");
		
		return currentEventId;
	}


	public void setCurrentEventId(String currentEventId) {
		this.currentEventId = currentEventId;
	}


	public String getCurrentPositionId() {
		return currentPositionId;
	}


	public void setCurrentPositionId(String currentPositionId) {
		this.currentPositionId = currentPositionId;
	}


	public boolean isAddedToNewEvent() {
		return isAddedToNewEvent;
	}


	public void setAddedToNewEvent(boolean isAddedToNewEvent) {
		this.isAddedToNewEvent = isAddedToNewEvent;
	}


	public void setVolunteerService(VolunteerService volunteerService) {
		this.volunteerService = volunteerService;
	}

}
